package com.oristartech.ori.common.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oristartech.ori.common.dto.Page;
import com.oristartech.ori.common.mapper.OrstMapper;
import com.oristartech.ori.common.mybatis.Criteria;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

/**
 * 通用基础服务实现类
 *
 * @author chenfenghai
 * @date 2019/02/28
 */
public class OrstServiceImpl<M extends OrstMapper<T>, T> implements OrstService<T> {

    @Autowired
    protected M orstMapper;

    public M getBaseMapper() {
        return orstMapper;
    }

    /**
     * 插入一条记录
     *
     * @param entity
     *            实体对象
     * @return
     */
    @Override
    public int insert(T entity) {
        return orstMapper.insert(entity);
    }

    /**
     * 批量插入记录
     *
     * @param list
     */
    @Override
    public void batchInsert(List<T> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        orstMapper.batchInsert(list);
    }

    /**
     * 插入一条记录，存在有效值的字段
     *
     * @param entity
     * @return
     */
    @Override
    public int insertSelective(T entity) {
        return orstMapper.insertSelective(entity);
    }

    /**
     * 批量插入记录，存在有效值的字段
     *
     * @param list
     */
    @Override
    public void batchInsertSelective(List<T> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        orstMapper.batchInsertSelective(list);
    }

    /**
     * 根据 ID删除记录
     *
     * @param id
     *            主键ID
     * @return
     */
    @Override
    public int deleteById(Serializable id) {
        return orstMapper.deleteById(id);
    }

    /**
     * 批量根据 ID删除记录
     *
     * @param list
     */
    @Override
    public void batchDeleteById(List<Long> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        orstMapper.batchDeleteById(list);
    }

    /**
     * 根据 UID删除
     *
     * @param uid
     * @return
     */
    @Override
    public int deleteByUid(String uid) {
        return orstMapper.deleteByUid(uid);
    }

    /**
     * 批量根据 UID删除记录
     *
     * @param list
     */
    @Override
    public void batchDeleteByUid(List<String> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        orstMapper.batchDeleteByUid(list);
    }

    /**
     * 根据 ID修改
     *
     * @param entity
     * @return
     */
    @Override
    public int updateById(T entity) {
        return orstMapper.updateById(entity);
    }

    /**
     * 批量根据 ID修改记录
     *
     * @param list
     */
    @Override
    public void batchUpdateById(List<T> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        orstMapper.batchUpdateById(list);
    }

    /**
     * 根据ID修改实体对象有效值的字段
     *
     * @param entity
     * @return
     */
    @Override
    public int updateSelectiveById(T entity) {
        return orstMapper.updateSelectiveById(entity);
    }

    /**
     * 批量根据ID修改实体对象有效值的字段
     *
     * @param list
     */
    @Override
    public void batchUpdateSelectiveById(List<T> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        orstMapper.batchUpdateSelectiveById(list);
    }

    /**
     * 根据UID修改
     *
     * @param entity
     * @return
     */
    @Override
    public int updateByUid(T entity) {
        return orstMapper.updateByUid(entity);
    }

    /**
     * 批量根据UID修改记录
     *
     * @param list
     */
    @Override
    public void batchUpdateByUid(List<T> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        orstMapper.batchUpdateByUid(list);
    }

    /**
     * 根据UID修改实体对象有效值的字段
     *
     * @param entity
     * @return
     */
    @Override
    public int updateSelectiveByUid(T entity) {
        return orstMapper.updateSelectiveByUid(entity);
    }

    /**
     * 批量根据UID修改实体对象有效值的字段
     *
     * @param list
     */
    @Override
    public void batchUpdateSelectiveByUid(List<T> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        orstMapper.batchUpdateSelectiveByUid(list);
    }

    /**
     * 根据 ID查询记录
     *
     * @param id
     * @return
     */
    @Override
    public T selectById(Serializable id) {
        return orstMapper.selectById(id);
    }

    /**
     * 根据 UID查询记录
     *
     * @param uid
     * @return
     */
    @Override
    public T selectByUid(String uid) {
        return orstMapper.selectByUid(uid);
    }

    /**
     * 根据对象有效值查询列表
     *
     * @param entity
     * @return
     */
    @Override
    public List<T> selectListSelective(T entity) {
        return orstMapper.selectListSelective(entity);
    }

    /**
     * 根据对象有效值查询一条记录
     *
     * @param entity
     * @return
     */
    @Override
    public T selectOneSelective(T entity) {
        return orstMapper.selectOneSelective(entity);
    }

    /**
     * 根据条件更新
     *
     * @param entity
     * @param criteria
     * @return
     */
    @Override
    public int updateSelective(T entity, Criteria criteria) {
        return orstMapper.updateSelective(entity, criteria);
    }

    /**
     * 根据条件查询一条记录
     *
     * @param criteria
     * @return
     */
    @Override
    public T selectOne(Criteria criteria) {
        return orstMapper.selectOne(criteria);
    }

    /**
     * 根据条件查询列表
     *
     * @param criteria
     * @return
     */
    @Override
    public List<T> selectList(Criteria criteria) {
        return orstMapper.selectList(criteria);
    }

    /**
     * 根据对象有效值分页查询列表
     *
     * @param page
     *            分页对象
     * @param entity
     *            实体对象
     * @return
     */
    @Override
    public PageInfo<T> selectPageSelective(Page<T> page, T entity) {
        return PageHelper.startPage(page.getPageNum(), page.getPageSize())
            .doSelectPageInfo(() -> orstMapper.selectListSelective(entity));
    }

    /**
     * 根据条件分页查询列表
     *
     * @param page
     *            分页对象
     * @param criteria
     *            查询条件
     * @return
     */
    @Override
    public PageInfo<T> selectPage(Page<T> page, Criteria criteria) {
        return PageHelper.startPage(page.getPageNum(), page.getPageSize())
            .doSelectPageInfo(() -> orstMapper.selectList(criteria));
    }
}
